package cn.iocoder.yudao.module.pplan.controller.admin.quarterproducerecord;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.distm.controller.admin.quarterplan.vo.QuarterPlanPageReqVO;
import cn.iocoder.yudao.module.distm.controller.admin.quarterplan.vo.QuarterPlanRespVO;
import cn.iocoder.yudao.module.distm.dal.dataobject.quarterplan.QuarterPlanDO;
import cn.iocoder.yudao.module.distm.service.quarterplan.QuarterPlanService;
import cn.iocoder.yudao.module.pplan.controller.admin.quarterproducerecord.vo.QuarterProduceRecordPageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.quarterproducerecord.vo.QuarterProduceRecordRespVO;
import cn.iocoder.yudao.module.pplan.controller.admin.quarterproducerecord.vo.QuarterProduceRecordSaveReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.quarterproducerecord.vo.QuarterProduceRecordUpdateStatusReqVO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.quarterproducerecord.QuarterProduceRecordDO;
import cn.iocoder.yudao.module.pplan.service.quarterproducerecord.QuarterProduceRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 生产计划-生产季度计划记录")
@RestController
@RequestMapping("/pplan/quarter-produce-record")
@Validated
public class QuarterProduceRecordController {

    @Resource
    private QuarterProduceRecordService quarterProduceRecordService;
    @Resource
    private QuarterPlanService quarterPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建生产计划-生产季度计划记录")
    @PreAuthorize("@ss.hasPermission('pplan:quarter-produce-record:create')")
    public CommonResult<Long> createQuarterProduceRecord(@Valid @RequestBody QuarterProduceRecordSaveReqVO createReqVO) {
        return success(quarterProduceRecordService.createQuarterProduceRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产计划-生产季度计划记录")
    @PreAuthorize("@ss.hasPermission('pplan:quarter-produce-record:update')")
    public CommonResult<Boolean> updateQuarterProduceRecord(@Valid @RequestBody QuarterProduceRecordSaveReqVO updateReqVO) {
        quarterProduceRecordService.updateQuarterProduceRecord(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新机供品餐具清洗计划清洗状态")
    public CommonResult<Boolean> updateQuarterProduceStatus(@Valid @RequestBody QuarterProduceRecordUpdateStatusReqVO updateReqVO) {
        quarterProduceRecordService.updateQuarterProduceStatus(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产计划-生产季度计划记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pplan:quarter-produce-record:delete')")
    public CommonResult<Boolean> deleteQuarterProduceRecord(@RequestParam("id") Long id) {
        quarterProduceRecordService.deleteQuarterProduceRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除生产计划-生产季度计划记录")
                @PreAuthorize("@ss.hasPermission('pplan:quarter-produce-record:delete')")
    public CommonResult<Boolean> deleteQuarterProduceRecordList(@RequestParam("ids") List<Long> ids) {
        quarterProduceRecordService.deleteQuarterProduceRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产计划-生产季度计划记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pplan:quarter-produce-record:query')")
    public CommonResult<QuarterProduceRecordRespVO> getQuarterProduceRecord(@RequestParam("id") Long id) {
        QuarterProduceRecordDO quarterProduceRecord = quarterProduceRecordService.getQuarterProduceRecord(id);
        return success(BeanUtils.toBean(quarterProduceRecord, QuarterProduceRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产计划-生产季度计划记录分页")
    @PreAuthorize("@ss.hasPermission('pplan:quarter-produce-record:query')")
    public CommonResult<PageResult<QuarterPlanRespVO>> getQuarterPlanPage(@Valid QuarterPlanPageReqVO pageReqVO) {
        PageResult<QuarterPlanDO> pageResult = quarterPlanService.getQuarterPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QuarterPlanRespVO.class));
    }
    //public CommonResult<PageResult<QuarterProduceRecordRespVO>> getQuarterProduceRecordPage(@Valid QuarterProduceRecordPageReqVO pageReqVO) {
    //    PageResult<QuarterProduceRecordDO> pageResult = quarterProduceRecordService.getQuarterProduceRecordPage(pageReqVO);
    //    return success(BeanUtils.toBean(pageResult, QuarterProduceRecordRespVO.class));
    //}

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产计划-生产季度计划记录 Excel")
    @PreAuthorize("@ss.hasPermission('pplan:quarter-produce-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQuarterProduceRecordExcel(@Valid QuarterProduceRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QuarterProduceRecordDO> list = quarterProduceRecordService.getQuarterProduceRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产计划-生产季度计划记录.xls", "数据", QuarterProduceRecordRespVO.class,
                        BeanUtils.toBean(list, QuarterProduceRecordRespVO.class));
    }

}